﻿/**************************************************************
 *
 * 命名空间：Sgr.UPMS.Infrastructure.EntityConfigurations
 * 创建时间：2025/3/20
 * 描述：菜单实体配置
 *
 **************************************************************/

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Sgr.Data;
using Sgr.EntityFrameworkCore.EntityConfigurations;
using Sgr.UPMS.Domain.Menus;

namespace Sgr.UPMS.Infrastructure.EntityConfigurations
{
    /// <summary>
    /// 菜单实体类型配置
    /// </summary>
    internal class MenuEntityTypeConfiguration : EntityTypeConfigurationBase<Menu, long>
    {
        public MenuEntityTypeConfiguration(DatabaseType? databaseType = null) : base(databaseType)
        { }

        public override void Configure(EntityTypeBuilder<Menu> builder)
        {
            builder.ToTable("upms_menus");

            base.Configure(builder);

            // 配置基本字段
            builder.PropertyAndHasColumnName(b => b.Code, GetColumnNameCase())
                .HasMaxLength(255)
                .IsRequired()
                .HasComment("菜单编码");

            builder.PropertyAndHasColumnName(b => b.Name, GetColumnNameCase())
                .HasMaxLength(255)
                .IsRequired()
                .HasComment("菜单名称");

            builder.PropertyAndHasColumnName(b => b.Type, GetColumnNameCase())
                .IsRequired()
                .HasMaxLength(20)
                .HasComment("菜单类型：Directory-目录，Menu-菜单，Button-按钮");

            builder.PropertyAndHasColumnName(b => b.Icon, GetColumnNameCase())
                .HasMaxLength(255)
                .HasComment("菜单图标");

            builder.PropertyAndHasColumnName(b => b.RouteName, GetColumnNameCase())
                .HasMaxLength(255)
                .HasComment("路由名称");

            builder.PropertyAndHasColumnName(b => b.RoutePath, GetColumnNameCase())
                .HasMaxLength(255)
                .HasComment("路由路径");

            builder.PropertyAndHasColumnName(b => b.ComponentPath, GetColumnNameCase())
                .HasMaxLength(255)
                .HasComment("组件路径");

            builder.PropertyAndHasColumnName(b => b.RedirectPath, GetColumnNameCase())
                .HasMaxLength(255)
                .HasComment("重定向路径");

            builder.PropertyAndHasColumnName(b => b.Permission, GetColumnNameCase())
                .HasMaxLength(255)
                .HasComment("权限标识");

            builder.PropertyAndHasColumnName(b => b.Description, GetColumnNameCase())
                .HasMaxLength(500)
                .HasComment("菜单描述");

            builder.PropertyAndHasColumnName(b => b.Order, GetColumnNameCase())
                .HasDefaultValue(0)
                .HasComment("排序号");

            builder.PropertyAndHasColumnName(b => b.IsActive, GetColumnNameCase())
                .IsRequired()
                .HasDefaultValue(true)
                .HasComment("是否激活：true-是，false-否");

            builder.PropertyAndHasColumnName(b => b.IsDeactivateByParent, GetColumnNameCase())
                .IsRequired()
                .HasDefaultValue(false)
                .HasComment("是否因父菜单停用而停用：true-是，false-否");

            builder.PropertyAndHasColumnName(b => b.ExternalLink, GetColumnNameCase())
                .IsRequired()
                .HasDefaultValue(false)
                .HasComment("是否外部链接：true-是，false-否");

            builder.PropertyAndHasColumnName(b => b.Hidden, GetColumnNameCase())
                .IsRequired()
                .HasDefaultValue(false)
                .HasComment("是否隐藏：true-是，false-否");

            builder.PropertyAndHasColumnName(b => b.KeepAlive, GetColumnNameCase())
                .IsRequired()
                .HasDefaultValue(false)
                .HasComment("是否缓存：true-是，false-否");

            builder.PropertyAndHasColumnName(b => b.Affix, GetColumnNameCase())
                .IsRequired()
                .HasDefaultValue(false)
                .HasComment("是否固定：true-是，false-否");

            builder.PropertyAndHasColumnName(b => b.Target, GetColumnNameCase())
                .HasMaxLength(20)
                .HasComment("目标窗口：Self-当前窗口，Blank-新窗口");

            // 配置树形结构字段
            builder.PropertyAndHasColumnName(b => b.ParentId, GetColumnNameCase())
                .IsRequired()
                .HasDefaultValue(0)
                .HasComment("父级菜单ID");

            builder.PropertyAndHasColumnName(b => b.NodePath, GetColumnNameCase())
                .HasMaxLength(1000)
                .IsRequired()
                .HasComment("节点路径");

            // 添加索引
            // 树形结构索引
            base.AddTreeNodeIndex(builder);

            // 同级菜单名称唯一索引
            builder.HasIndex(b => new { b.ParentId, b.Name })
                .IsUnique()
                .HasDatabaseName("UK_Menu_ParentId_Name");

            // 添加唯一索引
            builder.HasIndex(b => b.RouteName)
                .IsUnique()
                .HasDatabaseName("UK_Menu_RouteName");
        }
    }
}